<?php
namespace App\Services\Fund;

use App\Services\Fund\IntegralAccount\IntegralAccountLogic;
use Request;
use App\Services\User\User;
use App\Services\User\Token;
use App\Services\User\App;
use App\Services\Manage\Api;
/**
 * 积分
 * Class IntegralAccount
 * @package App\Services\Fund
 */
class IntegralAccount
{
       //封装一层检验user_info、opposite_info后再执行stepRun <该页多数方法都需检验这两个json数据>
    protected function _checkJsonThenRun(array $data, string $method, string $scene = ''): array 
    {
        if($data['user_info'] !== '{}'){ $data['user_info'] = json_encode($data['user_info']); }
        if($data['opposite_info'] !== '{}'){ $data['opposite_info'] = json_encode($data['opposite_info']); }
        return (new IntegralAccountLogic())->$method($data);
    }
    
    /*兑换钱*/
//    public function exchange($data): array
//    {
//        $data = Api::addAppId($data);
//        ValidateIntegralAccount::checkValidate($data, ['exchange', 'money','app_id','currency_scale']);
//        return (new IntegralAccountLogic())->exchange($data);
//    }

    # 积分 初始化积分账户
   /* public function init(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'create_ip'=>' ', 'app_id'=>' ']);
        return $this->stepRun($data, 'init', 'init');
    }*/

    # 积分 充值 系统->a
    public function prepaid($data): array
    {
        $data = Api::addAppId($data);
        $data['update_ip'] = request()->ip();
        $data = init_array($data,['user_id'=>'0', 'integral'=>'0', 'update_ip'=>' ', 'app_id'=>' ', 'user_info'=>'{}','opposite_info'=>'{}', 'remark'=>'']);
        if(\SYSTEM_ID == $data['user_id']){
            return \verify_err($data, '不能为系统用户充值');
        }
        ValidateIntegralAccount::checkValidate($data, ['user_id', 'integral', 'update_ip', 'app_id', 'user_info', 'opposite_info']);
        return $this->_checkJsonThenRun($data, 'prepaid');
    }

    /*积分充值 系统->a,b,c...*/
    /*public function prepaidByUids(): array 
    {
        $data = $this->haveIssetReceiveData(['update_ip'=>' ', 'app_id'=>' ', 'user_infos'=>[],'opposite_info'=>'{}']);
        if(count($data['user_infos']) == 0){
            return auto_success(null, '没有满足可以派发积分的用户');
        }
        if($data['opposite_info'] !== '{}'){ $data['opposite_info'] = json_encode($data['opposite_info']); }
        $validate = validate($this->integralAccountValidate);
        if(!$validate->scene('prepaidByUids')->check($data)){
            return verify_err(null, $validate->getError());
        }
        foreach ($data['user_infos'] as $k => $v) {
            if(!isset($v['id'], $v['integral'])){ 
                return verify_err(null, '用户信息与充值积分个数信息不全'); 
            }
            if(\SYSTEM_ID == $v['id']){
                return \verify_err($v, '不能为系统用户充值');
            }
            if(!is_int($v['integral']) || $v['integral'] < 0){
                return verify_err($v, '积分个数错误');
            }
        }
        return $this->logic($this->integralAccountLogic)->prepaidByUids($data);
    }*/

    # 积分 转账 a->b
   /* public function transfer(): array
    {
        $data = $this->haveIssetReceiveData(['user_id'=>'0', 'opposite_id'=>'0', 'integral'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}']);
        return $this->_checkJsonThenRun($data, 'transfer', 'transfer');
    }*/

//    # 积分 冻结 a
//    public function freeze($data): array
//    {
//        $data = Api::addAppId($data);
//        $data['update_ip'] = request()->ip();
//        $data['opposite_id'] = SYSTEM_ID;
//        $user = (new User())->infoAll(['id'=>$data['user_id']]);
//        $data['user_info'] = $user['res'];
//        $token = (new Token())->hGetAll($data['token']);
//        $data['opposite_info'] = $token['res'];
//        ValidateIntegralAccount::checkValidate($data, ['user_id', 'opposite_id', 'integral', 'update_ip', 'app_id', 'user_info', 'opposite_info','type','obj_id']);
//        return $this->_checkJsonThenRun($data, 'freeze');
//    }

    /*后台管理员冻结某用户积分*/
    public function adminFreeze($data): array 
    {
        $app_key = config('tpproject.yayi_admin_app_key');
        $data['update_ip'] = Request::getClientIp();
        $data['opposite_id'] = SYSTEM_ID;
        $ids = $data['user_id'] . ',' . $data['opposite_id'];
        $userInfo = (new User())->infoByIds(['ids'=>$ids]);
        if($userInfo['res'][0]['status'] != SUCCESS_STATUS){
            return $userInfo;
        }
        $find = (new App())->find(['app_key'=>$app_key]);
        if($find['status'] != SUCCESS_STATUS){
            return $find ;
        }
        $data['app_id'] = $find['res']['id'];
        $data['user_info'] = json_encode($userInfo['res'][0]);
        $data['opposite_info'] = json_encode($find['res']);
        $data = init_array($data,['user_id'=>'0', 'opposite_id'=>'0', 'integral'=>'0', 'update_ip'=>' ', 'app_id'=>'0', 'user_info'=>'{}','opposite_info'=>'{}','reason'=>' ']);
        ValidateIntegralAccount::checkValidate($data, ['user_id', 'opposite_id', 'integral', 'update_ip', 'app_id', 'borrow_log_id', 'freeze_log_id', 'user_info', 'opposite_info','type','obj_id']);
        return (new IntegralAccountLogic())->adminFreeze($data);
    }

    # 积分 解冻 a
    public function unfreeze($data): array
    {
        $data = Api::addAppId($data);
        $data['update_ip'] = request()->ip();
        $data['type'] = 'system';
        $data['freeze_counts'] = 1;
        $user = (new User())->infoAll(['id'=>$data['user_id']]);
        $data['opposite_id'] = $user['res']['id'];
        $data['user_info'] = $user['res'];
        $token = (new Token())->hGetAll($data['token']);
        $data['opposite_info'] = $token['res'];
        $data['user_info'] = json_encode($data['user_info']);
        $data['opposite_info'] = json_encode($data['opposite_info']);
        ValidateIntegralAccount::checkValidate($data, ['user_id', 'opposite_id', 'integral', 'update_ip', 'app_id', 'borrow_log_id', 'freeze_log_id', 'user_info', 'opposite_info']);
        return (new IntegralAccountLogic())->unfreeze($data);
    }

    # 积分 获取列表
    public function list($data): array
    {
        $data = init_array($data,['page'=>'1', 'list_rows'=>'', 'keywords'=>'']);
        ValidateIntegralAccount::checkValidate($data, ['page', 'list_rows']);
        return (new IntegralAccountLogic())->list($data);
    }

    # 积分 某user_ids的用户资金列表
    public function listByUserIds($data): array
    {
        $data = init_array($data,['user_ids'=>' ']);
        ValidateIntegralAccount::checkValidate($data, ['user_ids']);
        return (new IntegralAccountLogic())->listByUserIds($data);
    }

    /*获取某订单实际能抵扣的积分信息*/
   /* public function getIntegralDeductionInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['user_available_integral'=>0, 'order_max_integral_deduction'=>0, 'order_total_price'=>0, 'order_currency_total_price'=>0, 'fund_type'=>' ', 'app_id'=>0, 'currency_scale'=>'']);
        return $this->stepRun($data, 'getIntegralDeductionInfo');
    }*/

    /*获取某证书的支付详情 前提为：使用积分进行了抵扣*/
   /* public function getCertiPayInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['user_available_integral'=>0, 'certi_max_integral_deduction'=>0, 'certi_total_price'=>0, 'app_id'=>0]);
        return $this->stepRun($data, 'getCertiPayInfo');
    }*/

}